package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.ISearchGoodsListView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2018/12/6
 * @Describe
 */
public class SearchGoodsListPresenter extends BasePresenter<ISearchGoodsListView> {
    public SearchGoodsListPresenter(ISearchGoodsListView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }


    /**
     * 搜索商品列表
     * @param page
     * @param searchMsg
     */
    public void searchGoodsData(final int page, String searchMsg) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("search", searchMsg);
        params.put("limit", "10");
        params.put("page", page+"");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).searchGoods(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<GoodsInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<GoodsInfo> result) {
                        if (result.isSuccess()){
                            if (null != result.getData() && result.getData().size() > 0){
                                view.onGoodsInfoListSuccess(result.getData());
                            }else {
                                if (page == 1) {
                                    view.onEmpty("暂无数据");
                                }else {
                                    view.onLoadMoreEnd("加载完毕");
                                }
                            }
                        }else {
                            if (page == 1) {
                                view.onLoadError("网络异常");
                            }else {
                                view.onLoadMoreError("加载失败");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (page == 1) {
                            view.onLoadError("网络异常");
                        }else {
                            view.onLoadMoreError("加载失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
